OpenTelemetryのプロトコルで通信するGo言語のテストプログラムの紹介

# はじめに

OpenTelemetryのプロトコルでテレメトリーデータ（メトリクス、ログ、トレース）を送受信するテストプログラムを作りました。その紹介です。


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3874862/bc2b35b8-2da4-4a71-9735-b77e9a3f40f4.png)



# OpenTelemetry

OpenTelemetryは、ソフトウェアのパフォーマンスや動作の分析のための技術です。
テレメトリーデータ（メトリクス、ログ、トレース）の計装、生成、収集、エクスポートを実現するための
API、SDK、ツールを提供しています。

クラウドサービスなどを観測可能する技術です。

https://opentelemetry.io/ja/


# 受信側

OpenTelemetryのコレクター

https://opentelemetry.io/ja/docs/collector/

で受信する例は多数ありますが、受信するプログラムの例はほとんどありません。
コレクターのソースコード

https://github.com/open-telemetry/opentelemetry-collector


にOTLPの受信パッケージがあります。

https://github.com/open-telemetry/opentelemetry-collector/tree/main/receiver/otlpreceiver

このパッケージを使って受信プログラムを作りました。

```go:main.go
package main

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"log"

	"go.opentelemetry.io/collector/client"
	"go.opentelemetry.io/collector/component"
	"go.opentelemetry.io/collector/component/componenttest"
	"go.opentelemetry.io/collector/consumer"
	"go.opentelemetry.io/collector/pdata/plog"
	"go.opentelemetry.io/collector/pdata/pmetric"
	"go.opentelemetry.io/collector/pdata/ptrace"
	"go.opentelemetry.io/collector/receiver"
	"go.opentelemetry.io/collector/receiver/otlpreceiver"
)

func main() {
	f := otlpreceiver.NewFactory()
	config := f.CreateDefaultConfig()
	if c, ok := config.(*otlpreceiver.Config); ok {
		c.HTTP.ServerConfig.Endpoint = "0.0.0.0:4318"
	}
	log.Printf("config=%#v", config)
	componentID := component.MustNewID("otlp")
	ctx := context.Background()
	// METRICS
	log.Println("start metrics")
	mr, err := consumer.NewMetrics(func(ctx context.Context, md pmetric.Metrics) error {
		f := client.FromContext(ctx)
		log.Printf("recv metric from %s", f.Addr.String())
		jm := pmetric.JSONMarshaler{}
		if js, err := jm.MarshalMetrics(md); err == nil {
			printJSON(js)
		} else {
			log.Printf("err=%v", err)
		}
		return nil
	})
	if err != nil {
		log.Fatalln(err)
	}
	_, err = f.CreateMetrics(ctx,
		receiver.Settings{
			ID:                componentID,
			TelemetrySettings: componenttest.NewNopTelemetrySettings(),
		},
		config, mr)
	if err != nil {
		log.Fatalf("Failed to create receiver otlp: %v", err)
	}
	// TRACES
	log.Println("start traces")
	tr, err := consumer.NewTraces(func(ctx context.Context, td ptrace.Traces) error {
		f := client.FromContext(ctx)
		log.Printf("recv trace from %s", f.Addr.String())
		jm := ptrace.JSONMarshaler{}
		if js, err := jm.MarshalTraces(td); err == nil {
			printJSON(js)
		} else {
			log.Printf("err=%v", err)
		}
		return nil
	})
	if err != nil {
		log.Fatalln(err)
	}
	_, err = f.CreateTraces(ctx,
		receiver.Settings{
			ID:                componentID,
			TelemetrySettings: componenttest.NewNopTelemetrySettings(),
		},
		config, tr)
	if err != nil {
		log.Fatalf("Failed to create receiver otlp: %v", err)
	}
	// LOGS
	log.Println("start logs")
	lr, err := consumer.NewLogs(func(ctx context.Context, ld plog.Logs) error {
		f := client.FromContext(ctx)
		log.Printf("recv log form %s", f.Addr.String())
		jm := plog.JSONMarshaler{}
		if js, err := jm.MarshalLogs(ld); err == nil {
			printJSON(js)
		} else {
			log.Printf("err=%v", err)
		}
		return nil
	})
	if err != nil {
		log.Fatalln(err)
	}
	logReceiver, err := f.CreateLogs(ctx,
		receiver.Settings{
			ID:                componentID,
			TelemetrySettings: componenttest.NewNopTelemetrySettings(),
		},
		config, lr)
	if err != nil {
		log.Fatalf("Failed to create receiver otlp: %v", err)
	}
	err = logReceiver.Start(ctx, nil)
	if err != nil {
		log.Fatalln(err)
	}
	// Keep the server running
	select {}
}

func printJSON(j []byte) {
	var dst bytes.Buffer
	if err := json.Indent(&dst, j, "", "  "); err == nil {
		fmt.Println(dst.String())
		return
	}
	fmt.Println(string(j))
}

```

ポイントは、

- `otlpreceiver.NewFactor`でFactoryを作る
- Factoryでデフォルト設定を作る
- デフォルト設定を変更する
- Metric,Trace,Logの処理ルーチンをconsumerパッケージで作る
- Factoryで各受信サーバーを作る
- 受信サーバーを変更した設定で起動する

起動は、最後のログサーバーだけ
```go
	err = logReceiver.Start(ctx, nil)
	if err != nil {
		log.Fatalln(err)
	}
```
にします。MetriやTraceを起動してしまうと、後から登録した処理が
動作しません。


# 送信側

送信側は、各言語のSDKにGetting Startedがあります。Go言語の場合は

https://opentelemetry.io/ja/docs/languages/go/getting-started/

です。

このままでは、画面にメトリックなどが表示されるので、受信側にOTLPで送信するように、
このソースコードを少し変更します。

```diff_go:otel.go

func newtracerProvider() (*trace.TracerProvider, error) {
-	 traceExporter, err := stdouttrace.New(
-		stdouttrace.WithPrettyPrint())
+	traceExporter, err := otlptracehttp.New(context.Background(), otlptracehttp.WithEndpointURL("http://localhost:4318/v1/traces"))

	if err != nil {
		return nil, err
	}

	tracerProvider := trace.NewTracerProvider(
		trace.WithBatcher(traceExporter,
			// Default is 5s. Set to 1s for demonstrative purposes.
			trace.WithBatchTimeout(5*time.Second)),
	)
	return tracerProvider, nil
}

func newMeterProvider() (*metric.MeterProvider, error) {
- metricExporter, err := stdoutmetric.New()
+	metricExporter, err := otlpmetrichttp.New(context.Background(), otlpmetrichttp.WithEndpointURL("http://localhost:4318/v1/metrics"), otlpmetrichttp.WithInsecure())
	if err != nil {
		return nil, err
	}

	meterProvider := metric.NewMeterProvider(
		metric.WithReader(metric.NewPeriodicReader(metricExporter,
			// Default is 1m. Set to 3s for demonstrative purposes.
			metric.WithInterval(5*time.Second))),
	)
	return meterProvider, nil
}

func newLoggerProvider() (*log.LoggerProvider, error) {
-	logExporter, err := stdoutlog.New()
+	logExporter, err := otlploghttp.New(context.Background(), otlploghttp.WithEndpointURL("http://localhost:4318/v1/logs"))
	if err != nil {
		return nil, err
	}

	loggerProvider := log.NewLoggerProvider(
		log.WithProcessor(log.NewBatchProcessor(logExporter)),
	)
	return loggerProvider, nil
}


```

# 送受信の実験

受信側と受信側のプログラムを起動します。
他のターミナルから

```terminal
$curl http://localhost:8080/rolldice/
```

のようにアクセスします。

## メトリック

うまく受信できれば。画面に

```json
{
  "resourceMetrics": [
    {
      "resource": {
        "attributes": [
          {
            "key": "service.name",
            "value": {
              "stringValue": "dice"
            }
          },
          {
            "key": "service.version",
            "value": {
              "stringValue": "0.1.0"
            }
          },
          {
            "key": "telemetry.sdk.language",
            "value": {
              "stringValue": "go"
            }
          },
          {
            "key": "telemetry.sdk.name",
            "value": {
              "stringValue": "opentelemetry"
            }
          },
          {
            "key": "telemetry.sdk.version",
            "value": {
              "stringValue": "1.35.0"
            }
          }
        ]
      },
      "scopeMetrics": [
        {
          "scope": {
            "name": "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp",
            "version": "0.60.0"
          },
          "metrics": [
            {
              "name": "http.server.request.body.size",
              "description": "Size of HTTP server request bodies.",
              "unit": "By",
              "histogram": {
                "dataPoints": [
                  {
                    "attributes": [
                      {
                        "key": "http.request.method",
                        "value": {
                          "stringValue": "GET"
                        }
                      },
                      {
                        "key": "http.response.status_code",
                        "value": {
                          "intValue": "301"
                        }
                      },
                      {
                        "key": "network.protocol.name",
                        "value": {
                          "stringValue": "http"
                        }
                      },
                      {
                        "key": "network.protocol.version",
                        "value": {
                          "stringValue": "1.1"
                        }
                      },
                      {
                        "key": "server.address",
                        "value": {
                          "stringValue": "localhost"
                        }
                      },
                      {
                        "key": "server.port",
                        "value": {
                          "intValue": "8080"
                        }
                      },
                      {
                        "key": "url.scheme",
                        "value": {
                          "stringValue": "http"
                        }
                      }
                    ],
                    "startTimeUnixNano": "1748135663575828000",
                    "timeUnixNano": "1748138363568992000",
                    "count": "1",
                    "sum": 0,
                    "bucketCounts": [
                      "1",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0"
                    ],
                    "explicitBounds": [
                      0,
                      5,
                      10,
                      25,
                      50,
                      75,
                      100,
                      250,
                      500,
                      750,
                      1000,
                      2500,
                      5000,
                      7500,
                      10000
                    ],
                    "exemplars": [
                      {
                        "timeUnixNano": "1748135678099777000",
                        "asInt": "0",
                        "spanId": "61d063242a1dc225",
                        "traceId": "aa9927a4a9ccaa7c1cecb8210c6af052"
                      }
                    ],
                    "min": 0,
                    "max": 0
                  },
                  {
                    "attributes": [
                      {
                        "key": "http.request.method",
                        "value": {
                          "stringValue": "GET"
                        }
                      },
                      {
                        "key": "http.response.status_code",
                        "value": {
                          "intValue": "200"
                        }
                      },
                      {
                        "key": "http.route",
                        "value": {
                          "stringValue": "/rolldice/"
                        }
                      },
                      {
                        "key": "network.protocol.name",
                        "value": {
                          "stringValue": "http"
                        }
                      },
                      {
                        "key": "network.protocol.version",
                        "value": {
                          "stringValue": "1.1"
                        }
                      },
                      {
                        "key": "server.address",
                        "value": {
                          "stringValue": "localhost"
                        }
                      },
                      {
                        "key": "server.port",
                        "value": {
                          "intValue": "8080"
                        }
                      },
                      {
                        "key": "url.scheme",
                        "value": {
                          "stringValue": "http"
                        }
                      }
                    ],
                    "startTimeUnixNano": "1748135663575828000",
                    "timeUnixNano": "1748138363568992000",
                    "count": "1",
                    "sum": 0,
                    "bucketCounts": [
                      "1",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0"
                    ],
                    "explicitBounds": [
                      0,
                      5,
                      10,
                      25,
                      50,
                      75,
                      100,
                      250,
                      500,
                      750,
                      1000,
                      2500,
                      5000,
                      7500,
                      10000
                    ],
                    "exemplars": [
                      {
                        "timeUnixNano": "1748135681063011000",
                        "asInt": "0",
                        "spanId": "9a958b005350bece",
                        "traceId": "dcb1ea50a33893d84b8b31fb25e888c9"
                      }
                    ],
                    "min": 0,
                    "max": 0
                  }
                ],
                "aggregationTemporality": 2
              }
            },
            {
              "name": "http.server.response.body.size",
              "description": "Size of HTTP server response bodies.",
              "unit": "By",
              "histogram": {
                "dataPoints": [
                  {
                    "attributes": [
                      {
                        "key": "http.request.method",
                        "value": {
                          "stringValue": "GET"
                        }
                      },
                      {
                        "key": "http.response.status_code",
                        "value": {
                          "intValue": "301"
                        }
                      },
                      {
                        "key": "network.protocol.name",
                        "value": {
                          "stringValue": "http"
                        }
                      },
                      {
                        "key": "network.protocol.version",
                        "value": {
                          "stringValue": "1.1"
                        }
                      },
                      {
                        "key": "server.address",
                        "value": {
                          "stringValue": "localhost"
                        }
                      },
                      {
                        "key": "server.port",
                        "value": {
                          "intValue": "8080"
                        }
                      },
                      {
                        "key": "url.scheme",
                        "value": {
                          "stringValue": "http"
                        }
                      }
                    ],
                    "startTimeUnixNano": "1748135663575835000",
                    "timeUnixNano": "1748138363569008000",
                    "count": "1",
                    "sum": 45,
                    "bucketCounts": [
                      "0",
                      "0",
                      "0",
                      "0",
                      "1",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0"
                    ],
                    "explicitBounds": [
                      0,
                      5,
                      10,
                      25,
                      50,
                      75,
                      100,
                      250,
                      500,
                      750,
                      1000,
                      2500,
                      5000,
                      7500,
                      10000
                    ],
                    "exemplars": [
                      {
                        "timeUnixNano": "1748135678099779000",
                        "asInt": "45",
                        "spanId": "61d063242a1dc225",
                        "traceId": "aa9927a4a9ccaa7c1cecb8210c6af052"
                      }
                    ],
                    "min": 45,
                    "max": 45
                  },
                  {
                    "attributes": [
                      {
                        "key": "http.request.method",
                        "value": {
                          "stringValue": "GET"
                        }
                      },
                      {
                        "key": "http.response.status_code",
                        "value": {
                          "intValue": "200"
                        }
                      },
                      {
                        "key": "http.route",
                        "value": {
                          "stringValue": "/rolldice/"
                        }
                      },
                      {
                        "key": "network.protocol.name",
                        "value": {
                          "stringValue": "http"
                        }
                      },
                      {
                        "key": "network.protocol.version",
                        "value": {
                          "stringValue": "1.1"
                        }
                      },
                      {
                        "key": "server.address",
                        "value": {
                          "stringValue": "localhost"
                        }
                      },
                      {
                        "key": "server.port",
                        "value": {
                          "intValue": "8080"
                        }
                      },
                      {
                        "key": "url.scheme",
                        "value": {
                          "stringValue": "http"
                        }
                      }
                    ],
                    "startTimeUnixNano": "1748135663575835000",
                    "timeUnixNano": "1748138363569008000",
                    "count": "1",
                    "sum": 2,
                    "bucketCounts": [
                      "0",
                      "1",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0"
                    ],
                    "explicitBounds": [
                      0,
                      5,
                      10,
                      25,
                      50,
                      75,
                      100,
                      250,
                      500,
                      750,
                      1000,
                      2500,
                      5000,
                      7500,
                      10000
                    ],
                    "exemplars": [
                      {
                        "timeUnixNano": "1748135681063014000",
                        "asInt": "2",
                        "spanId": "9a958b005350bece",
                        "traceId": "dcb1ea50a33893d84b8b31fb25e888c9"
                      }
                    ],
                    "min": 2,
                    "max": 2
                  }
                ],
                "aggregationTemporality": 2
              }
            },
            {
              "name": "http.server.request.duration",
              "description": "Duration of HTTP server requests.",
              "unit": "s",
              "histogram": {
                "dataPoints": [
                  {
                    "attributes": [
                      {
                        "key": "http.request.method",
                        "value": {
                          "stringValue": "GET"
                        }
                      },
                      {
                        "key": "http.response.status_code",
                        "value": {
                          "intValue": "301"
                        }
                      },
                      {
                        "key": "network.protocol.name",
                        "value": {
                          "stringValue": "http"
                        }
                      },
                      {
                        "key": "network.protocol.version",
                        "value": {
                          "stringValue": "1.1"
                        }
                      },
                      {
                        "key": "server.address",
                        "value": {
                          "stringValue": "localhost"
                        }
                      },
                      {
                        "key": "server.port",
                        "value": {
                          "intValue": "8080"
                        }
                      },
                      {
                        "key": "url.scheme",
                        "value": {
                          "stringValue": "http"
                        }
                      }
                    ],
                    "startTimeUnixNano": "1748135663575838000",
                    "timeUnixNano": "1748138363569014000",
                    "count": "1",
                    "sum": 0.00022262499999999998,
                    "bucketCounts": [
                      "1",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0"
                    ],
                    "explicitBounds": [
                      0.005,
                      0.01,
                      0.025,
                      0.05,
                      0.075,
                      0.1,
                      0.25,
                      0.5,
                      0.75,
                      1,
                      2.5,
                      5,
                      7.5,
                      10
                    ],
                    "exemplars": [
                      {
                        "timeUnixNano": "1748135678099782000",
                        "asDouble": 0.00022262499999999998,
                        "spanId": "61d063242a1dc225",
                        "traceId": "aa9927a4a9ccaa7c1cecb8210c6af052"
                      }
                    ],
                    "min": 0.00022262499999999998,
                    "max": 0.00022262499999999998
                  },
                  {
                    "attributes": [
                      {
                        "key": "http.request.method",
                        "value": {
                          "stringValue": "GET"
                        }
                      },
                      {
                        "key": "http.response.status_code",
                        "value": {
                          "intValue": "200"
                        }
                      },
                      {
                        "key": "http.route",
                        "value": {
                          "stringValue": "/rolldice/"
                        }
                      },
                      {
                        "key": "network.protocol.name",
                        "value": {
                          "stringValue": "http"
                        }
                      },
                      {
                        "key": "network.protocol.version",
                        "value": {
                          "stringValue": "1.1"
                        }
                      },
                      {
                        "key": "server.address",
                        "value": {
                          "stringValue": "localhost"
                        }
                      },
                      {
                        "key": "server.port",
                        "value": {
                          "intValue": "8080"
                        }
                      },
                      {
                        "key": "url.scheme",
                        "value": {
                          "stringValue": "http"
                        }
                      }
                    ],
                    "startTimeUnixNano": "1748135663575838000",
                    "timeUnixNano": "1748138363569014000",
                    "count": "1",
                    "sum": 0.000214917,
                    "bucketCounts": [
                      "1",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0",
                      "0"
                    ],
                    "explicitBounds": [
                      0.005,
                      0.01,
                      0.025,
                      0.05,
                      0.075,
                      0.1,
                      0.25,
                      0.5,
                      0.75,
                      1,
                      2.5,
                      5,
                      7.5,
                      10
                    ],
                    "exemplars": [
                      {
                        "timeUnixNano": "1748135681063019000",
                        "asDouble": 0.000214917,
                        "spanId": "9a958b005350bece",
                        "traceId": "dcb1ea50a33893d84b8b31fb25e888c9"
                      }
                    ],
                    "min": 0.000214917,
                    "max": 0.000214917
                  }
                ],
                "aggregationTemporality": 2
              }
            }
          ]
        },
        {
          "scope": {
            "name": "go.opentelemetry.io/contrib/examples/dice"
          },
          "metrics": [
            {
              "name": "dice.rolls",
              "description": "The number of rolls by roll value",
              "unit": "{roll}",
              "sum": {
                "dataPoints": [
                  {
                    "attributes": [
                      {
                        "key": "roll.value",
                        "value": {
                          "intValue": "4"
                        }
                      }
                    ],
                    "startTimeUnixNano": "1748135663575014000",
                    "timeUnixNano": "1748138363569024000",
                    "asInt": "1",
                    "exemplars": [
                      {
                        "timeUnixNano": "1748135681062980000",
                        "asInt": "1",
                        "spanId": "a140e03294bc6177",
                        "traceId": "dcb1ea50a33893d84b8b31fb25e888c9"
                      }
                    ]
                  }
                ],
                "aggregationTemporality": 2,
                "isMonotonic": true
              }
            }
          ]
        }
      ],
      "schemaUrl": "https://opentelemetry.io/schemas/1.26.0"
    }
  ]
}

```


## トレース


```json
{
  "resourceSpans": [
    {
      "resource": {
        "attributes": [
          {
            "key": "service.name",
            "value": {
              "stringValue": "dice"
            }
          },
          {
            "key": "service.version",
            "value": {
              "stringValue": "0.1.0"
            }
          },
          {
            "key": "telemetry.sdk.language",
            "value": {
              "stringValue": "go"
            }
          },
          {
            "key": "telemetry.sdk.name",
            "value": {
              "stringValue": "opentelemetry"
            }
          },
          {
            "key": "telemetry.sdk.version",
            "value": {
              "stringValue": "1.35.0"
            }
          }
        ]
      },
      "scopeSpans": [
        {
          "scope": {
            "name": "go.opentelemetry.io/contrib/examples/dice"
          },
          "spans": [
            {
              "traceId": "e97bf9282e140a858ed353e09b888b76",
              "spanId": "2eee288011e4a1d1",
              "parentSpanId": "96823f1557447f6e",
              "flags": 256,
              "name": "roll",
              "kind": 1,
              "startTimeUnixNano": "1748138368385720000",
              "endTimeUnixNano": "1748138368385810625",
              "attributes": [
                {
                  "key": "roll.value",
                  "value": {
                    "intValue": "1"
                  }
                }
              ],
              "status": {}
            }
          ]
        },
        {
          "scope": {
            "name": "go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp",
            "version": "0.60.0"
          },
          "spans": [
            {
              "traceId": "e97bf9282e140a858ed353e09b888b76",
              "spanId": "96823f1557447f6e",
              "parentSpanId": "",
              "flags": 256,
              "name": "/",
              "kind": 2,
              "startTimeUnixNano": "1748138368385696000",
              "endTimeUnixNano": "1748138368385852209",
              "attributes": [
                {
                  "key": "server.address",
                  "value": {
                    "stringValue": "localhost"
                  }
                },
                {
                  "key": "http.request.method",
                  "value": {
                    "stringValue": "GET"
                  }
                },
                {
                  "key": "url.scheme",
                  "value": {
                    "stringValue": "http"
                  }
                },
                {
                  "key": "server.port",
                  "value": {
                    "intValue": "8080"
                  }
                },
                {
                  "key": "network.peer.address",
                  "value": {
                    "stringValue": "::1"
                  }
                },
                {
                  "key": "network.peer.port",
                  "value": {
                    "intValue": "58126"
                  }
                },
                {
                  "key": "user_agent.original",
                  "value": {
                    "stringValue": "curl/8.7.1"
                  }
                },
                {
                  "key": "client.address",
                  "value": {
                    "stringValue": "::1"
                  }
                },
                {
                  "key": "url.path",
                  "value": {
                    "stringValue": "/rolldice/"
                  }
                },
                {
                  "key": "network.protocol.version",
                  "value": {
                    "stringValue": "1.1"
                  }
                },
                {
                  "key": "http.route",
                  "value": {
                    "stringValue": "/rolldice/"
                  }
                },
                {
                  "key": "http.response.body.size",
                  "value": {
                    "intValue": "2"
                  }
                },
                {
                  "key": "http.response.status_code",
                  "value": {
                    "intValue": "200"
                  }
                }
              ],
              "status": {}
            }
          ]
        }
      ],
      "schemaUrl": "https://opentelemetry.io/schemas/1.26.0"
    }
  ]
}

```

## ログ

```json
{
  "resourceLogs": [
    {
      "resource": {
        "attributes": [
          {
            "key": "service.name",
            "value": {
              "stringValue": "dice"
            }
          },
          {
            "key": "service.version",
            "value": {
              "stringValue": "0.1.0"
            }
          },
          {
            "key": "telemetry.sdk.language",
            "value": {
              "stringValue": "go"
            }
          },
          {
            "key": "telemetry.sdk.name",
            "value": {
              "stringValue": "opentelemetry"
            }
          },
          {
            "key": "telemetry.sdk.version",
            "value": {
              "stringValue": "1.35.0"
            }
          }
        ]
      },
      "scopeLogs": [
        {
          "scope": {
            "name": "go.opentelemetry.io/contrib/examples/dice"
          },
          "logRecords": [
            {
              "timeUnixNano": "1748138368385731000",
              "observedTimeUnixNano": "1748138368385757000",
              "severityNumber": 9,
              "severityText": "INFO",
              "body": {
                "stringValue": "Anonymous player is rolling the dice"
              },
              "attributes": [
                {
                  "key": "result",
                  "value": {
                    "intValue": "1"
                  }
                }
              ],
              "flags": 1,
              "traceId": "e97bf9282e140a858ed353e09b888b76",
              "spanId": "2eee288011e4a1d1"
            }
          ]
        }
      ],
      "schemaUrl": "https://opentelemetry.io/schemas/1.26.0"
    }
  ]
}

```




# 余談

このプログラムを元にして、TWSNML FKにOpenTelemetryコレクター機能を
組み込みました。

トレースなどのレポートを以下のような感じで表示できます。

https://twsnmp.github.io/twsnmpfk/index_ja.html

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3874862/65ed3d4f-f121-45b9-b4a7-bb6d0e265933.png)


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3874862/549b8ff8-1561-4d18-9e6a-3ac1e8ae731d.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3874862/3edadb37-9e81-42d9-aa1d-27317ede37ac.png)