Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ForceFlush and Shutdown do not collect pending measurements #3209

Closed
MrAlias opened this issue Sep 20, 2022 · 1 comment · Fixed by #3220
Closed

ForceFlush and Shutdown do not collect pending measurements #3209

MrAlias opened this issue Sep 20, 2022 · 1 comment · Fixed by #3220
Assignees
Labels
area:metrics Part of OpenTelemetry Metrics bug Something isn't working pkg:SDK Related to an SDK package

Comments

@MrAlias
Copy link
Contributor

MrAlias commented Sep 20, 2022

Both the MeterProvider's ForceFlush and Shutdown methods say they flush pending telemetry:

// ForceFlush flushes all pending telemetry.

// Shutdown shuts down the MeterProvider flushing all pending telemetry and
// releasing any held computational resources.

They currently do not do this for PeriodicReaders.

Example

main.go:

package main

import (
	"context"
	"log"

	"go.opentelemetry.io/otel/exporters/stdout/stdoutmetric"
	"go.opentelemetry.io/otel/sdk/metric"
)

func main() {
	ctx := context.Background()

	exporter, err := stdoutmetric.New()
	if err != nil {
		log.Fatalln("failed to create stdout exporter:", err)
	}

	meterProvider := metric.NewMeterProvider(
		metric.WithReader(metric.NewPeriodicReader(exporter)),
	)
	meter := meterProvider.Meter("my-meter")

	counter, err := meter.SyncFloat64().Counter("my-counter")
	if err != nil {
		log.Fatalln("failed to initialize instrument:", err)
	}

	counter.Add(ctx, 12.0)
	log.Println("counted", 12.0)

	err = meterProvider.Shutdown(ctx)
	if err != nil {
		log.Fatalln("failed to shutdown:", err)
	}
	log.Println("shutdown")
}

go.mod:

go 1.19

require (
	go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.32.0
	go.opentelemetry.io/otel/sdk/metric v0.32.0
)

require (
	github.com/go-logr/logr v1.2.3 // indirect
	github.com/go-logr/stdr v1.2.2 // indirect
	go.opentelemetry.io/otel v1.10.0 // indirect
	go.opentelemetry.io/otel/metric v0.32.0 // indirect
	go.opentelemetry.io/otel/sdk v1.10.0 // indirect
	go.opentelemetry.io/otel/trace v1.10.0 // indirect
	golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
)

This outputs:

$ go run .
2022/09/20 16:50:56 counted 12
2022/09/20 16:50:56 shutdown

What I would expect to see:

go run .
2022/09/20 16:52:13 counted 12
{
	"Resource": null,
	"ScopeMetrics": [
		{
			"Scope": {
				"Name": "my-meter",
				"Version": "",
				"SchemaURL": ""
			},
			"Metrics": [
				{
					"Name": "my-counter",
					"Description": "",
					"Unit": "",
					"Data": {
						"DataPoints": [
							{
								"Attributes": [],
								"StartTime": "2022-09-20T16:52:13.742478572-07:00",
								"Time": "2022-09-20T16:52:14.742870752-07:00",
								"Value": 12
							}
						],
						"Temporality": "CumulativeTemporality",
						"IsMonotonic": true
					}
				}
			]
		}
	]
}
2022/09/20 16:52:15 shutdown
@MrAlias MrAlias added bug Something isn't working pkg:SDK Related to an SDK package area:metrics Part of OpenTelemetry Metrics labels Sep 20, 2022
@MrAlias MrAlias self-assigned this Sep 21, 2022
@MrAlias
Copy link
Contributor Author

MrAlias commented Sep 22, 2022

Verified the changes proposed in #3220 resolve this issue based on the example provided in the description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:metrics Part of OpenTelemetry Metrics bug Something isn't working pkg:SDK Related to an SDK package
Projects
None yet
1 participant