Package: otel-cli
opentelemetry-cli: human-friendly OpenTelemetry CLIProvides a CLI for crafting and sending telemetry data over OTLP (OpenTelemetry Line Protocol).RequirementsInstallationThere are several ways of running this CLI.Dockerdockerpullopentelemetry-cli:<version>You can specify a version like0.2.0or uselatestto get the most up-to-date version.Run latest version of the CLI in a container:# set OTEL_EXPORTER_OTLP_ENDPOINT to your OTel collector instanceexportOTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4317
dockerrun--rm-eOTEL_EXPORTER_OTLP_ENDPOINTopentelemetry-cli:latest--helpReplace--helpwith anyotelcommand, withoutotelitself.PyPIpipinstallotel-cliUsageFirst, defineOTEL_EXPORTER_OTLP_ENDPOINTin your shell and set it to the OTLP collector instance you want to use.
For a local collector, set this tohttp://127.0.0.1:4317like so:exportOTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4317SpansTo send a span, run:otelspan"span name"To set a different service name, use the--serviceflag:otelspan--service"My Service""span name"You can also pass custom start and end dates. These should benanosecondssince the epoch:SPAN_START_DATE=$(date--date"2 minutes ago"+%s%N)SPAN_END_DATE=$(date+%s%N)otelspan--start"$SPAN_START_DATE"--end"$SPAN_END_DATE""span name"By default, spans are reported with a status ofUNKNOWN. To pass a different status, use the--statusoption:otelspan--statusOK"successful span"otelspan--statusERROR"failed span"To add attributes to spans, use the--attribute|-aoption. It accepts attributes in akey=valueformat. Use multiple instances of this option to send multiple attributes.otelspan-a"my.foo=bar"-a"my.bar=baz""span name"otel will create a random trace ID and span ID. You can override those:otelspan--trace-id"4d999706756fd1859345f8dc6d0af218"--span-id"ac2a3b2b19ac602d"Sending multiple spans in a traceTo create a single trace with one root span and multiple child spans, we first need to generate a trace ID for the entire trace and a span ID for the parent span. Useotel generateto create those:TRACE_ID=$(otelgeneratetrace_id)PARENT_SPAN=$(otelgeneratespan_id)Then, when creating children span, we pass this information in the format of aTRACEPARENT:TRACEPARENT="00-${TRACE_ID}-${PARENT_SPAN}-01"otelspan--traceparent"$TRACEPARENT""Child A Name"otelspan--traceparent"$TRACEPARENT""Child B Name"Finally, send the parent span using the pre-generated IDs:otelspan--trace-id"$TRACE_ID"--span-id"$PARENT_SPAN""Parent Span Name"MetricsUseotel metricto send metric data. The following metric types are currently supported:CounterUpDownCounterCounterCounters are metrics that can count only up.
By specifying just the counter name, it will be incremented by 1:otelmetriccountermy-counterYou can specify a different value to increase by. For example, this will increase the counter by 1024:otelmetriccountertotal-bytes1024Counters support attributes just like spans, using the-a|--attributeoption.otelmetriccountermy-counter-a"host.name=localhost"By default, attributes are strings. You can set them to other types by using one of the following prefixes:int:- value will be converted to an integer.float:- value will be converted to a floating point number.bool:- value will be converted to a boolean.Values ofy,yes,t,true,on, and1are converted toTrue.Values ofn,no,f,false,off, and0are converted toFalse.Values arenotcase-sensitive.Example:otelmetriccountermy-counter\-a"key1=just a string"\-a"int:key2=10"\-a"float:key3=3.14"\-a"bool:key4=YES"UpDownCounterUpDownCounters are metrics that count up or down.
If not given a value, the UpDownCounter will increment by one:otelmetricupdownqueue-lengthYou can specify a different value to increase by. For example, this will increase the counter by 1024:otelmetricupdownmy-updowncounter1024To decrease the counter number, pass a negative number like so:otelmetricupdownqueue-length-1PackagingThis project usespoetryto manage dependencies, build, etc.